﻿using BaoShouTang.DBModel;
using BaoShouTangServer.DTOModel;
using BaoShouTangServer.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using UFIDA.U8.MomServiceCommon;
using UFIDA.U8.U8MOMAPIFramework;
using UFIDA.U8.U8APIFramework;
using UFIDA.U8.U8APIFramework.Meta;
using UFIDA.U8.U8APIFramework.Parameter;
using MSXML2;
using Microsoft.Ajax.Utilities;
using System.Reflection.Emit;
using BaoShouTangServer.Models;
using BaoShouTangServer.Service;
using Newtonsoft.Json;

namespace BaoShouTangServer.ApiServies
{
    public class rdrecord11Service
    {
        public static bool 生成材料出库(U8Login.clsLogin u8Login,List<Z_任务单实际投入> z_任务单实际投入List, ref string rdrecord11Id,ref string 材料出库单接口返回result)
        {
            //第二步：构造环境上下文对象，传入login，并按需设置其它上下文参数
            U8EnvContext envContext = new U8EnvContext();
            envContext.U8Login = u8Login;

          

            //第三步：设置API地址标识(Url)
            //当前API：添加新单据的地址标识为：U8API/MaterialOut/Add
            U8ApiAddress myApiAddress = new U8ApiAddress("U8API/MaterialOut/Add");


            //第四步：构造APIBroker
            U8ApiBroker broker = new U8ApiBroker(myApiAddress, envContext);

            //第五步：API参数赋值

            //给普通参数sVouchType赋值。此参数的数据类型为System.String，此参数按值传递，表示单据类型：11
            broker.AssignNormalValue("sVouchType", "11");



            //给BO表头参数DomHead赋值，此BO参数的业务类型为材料出库单，属表头参数。BO参数均按引用传递
            //提示：给BO表头参数DomHead赋值有两种方法

            DataTable Dt_data = new DataTable();

            DbHelperSQL.connectionString = u8Login.UFDataConnstringForNet;
            StringBuilder strMsg = new StringBuilder();
            StringBuilder Strsql = new StringBuilder();

            //创建临时表
            string StrHeadName = "tempdb..RecordOutQ" + Guid.NewGuid().ToString().Replace("-", "");
            string StrBodyName = "tempdb..RecordOutSQ" + Guid.NewGuid().ToString().Replace("-", "");
            Strsql.Append(" select  top 0  * into " + StrHeadName + " from RecordOutQ Where 1=1");
            Strsql.Append(" \n select  top 0  *  into " + StrBodyName + " from RecordOutSQ Where 1=1");
            DbHelperSQL.ExecuteSql(Strsql.ToString());
            int id = (int)SnowFlakeSingle.Instance.NextId();
            int zpurRkdHead = 材料出库头赋值(StrHeadName, id,  z_任务单实际投入List);
            int zpurRkdTailList = 材料出库体赋值(StrBodyName, id,  z_任务单实际投入List);
            //塞入临时表

            Strsql = new StringBuilder();
            Strsql.Append("select editprop='A',* from " + StrHeadName);
            DOMDocument domHead = Pub.getDom(Strsql.ToString(), u8Login.UfDbName);
            Strsql = new StringBuilder();
            Strsql.Append("select editprop='A',* from " + StrBodyName);
            DOMDocument domBody = Pub.getDom(Strsql.ToString(), u8Login.UfDbName);

            Strsql = new StringBuilder();
            Strsql.Append(" if not object_id('" + StrHeadName + "') is null drop table " + StrHeadName);
            Strsql.Append(" if not object_id('" + StrBodyName + "') is null drop table " + StrBodyName);

            DbHelperSQL.ExecuteSql(Strsql.ToString());

            broker.AssignNormalValue("DomHead", domHead);
            //方法一是直接传入MSXML2.DOMDocumentClass对象
            broker.AssignNormalValue("domBody", domBody);


            //给普通参数domPosition赋值。此参数的数据类型为System.Object，此参数按引用传递，表示货位：传空
            broker.AssignNormalValue("domPosition", null);

            //该参数errMsg为OUT型参数，由于其数据类型为System.String，为一般值类型，因此不必传入一个参数变量。在API调用返回时，可以通过GetResult("errMsg")获取其值

            //给普通参数cnnFrom赋值。此参数的数据类型为ADODB.Connection，此参数按引用传递，表示连接对象,如果由调用方控制事务，则需要设置此连接对象，否则传空
            broker.AssignNormalValue("cnnFrom", null);

            //该参数VouchId为INOUT型普通参数。此参数的数据类型为System.String，此参数按值传递。在API调用返回时，可以通过GetResult("VouchId")获取其值
            string VouchId = "";
            broker.AssignNormalValue("VouchId", VouchId);

            //该参数domMsg为OUT型参数，由于其数据类型为MSXML2.IXMLDOMDocument2，非一般值类型，因此必须传入一个参数变量。在API调用返回时，可以直接使用该参数
            MSXML2.DOMDocument domMsg = new MSXML2.DOMDocument();
            broker.AssignNormalValue("domMsg", domMsg);

            //给普通参数bCheck赋值。此参数的数据类型为System.Boolean，此参数按值传递，表示是否控制可用量。
            broker.AssignNormalValue("bCheck", false);

            //给普通参数bBeforCheckStock赋值。此参数的数据类型为System.Boolean，此参数按值传递，表示检查可用量
            broker.AssignNormalValue("bBeforCheckStock", false);

            //给普通参数bIsRedVouch赋值。此参数的数据类型为System.Boolean，此参数按值传递，表示是否红字单据
            broker.AssignNormalValue("bIsRedVouch", false);

            //给普通参数sAddedState赋值。此参数的数据类型为System.String，此参数按值传递，表示传空字符串
            broker.AssignNormalValue("sAddedState", "");

            //给普通参数bReMote赋值。此参数的数据类型为System.Boolean，此参数按值传递，表示是否远程：转入false
            broker.AssignNormalValue("bReMote", false);



            //第六步：调用API
            if (!broker.Invoke())
            {
                //错误处理
                Exception apiEx = broker.GetException();
                if (apiEx != null)
                {
                    if (apiEx is MomSysException)
                    {
                        MomSysException sysEx = apiEx as MomSysException;
                        材料出库单接口返回result = ("系统异常：" + sysEx.Message);
                        //todo:异常处理
                    }
                    else if (apiEx is MomBizException)
                    {
                        MomBizException bizEx = apiEx as MomBizException;
                        材料出库单接口返回result = ("API异常：" + bizEx.Message);
                        //todo:异常处理
                    }
                    //异常原因
                    String exReason = broker.GetExceptionString();
                    if (exReason.Length != 0)
                    {
                        材料出库单接口返回result = ("异常原因：" + exReason);
                    }
                }
                //结束本次调用，释放API资源
                broker.Release();
            }

            //第七步：获取返回结果

            //获取返回值
            //获取普通返回值。此返回值数据类型为System.String，此参数按值传递，表示错误描述：空，正确；非空，错误
            //System.String result = broker.GetReturnValue() as System.String;
            //材料出库单接口返回result = result;
            //获取普通OUT参数curID。此返回值数据类型为string，在使用该参数之前，请判断是否为空
            //string curIDRet = broker.GetResult("curID") as string;
            //获取普通OUT参数CurDom。此返回值数据类型为MSXML2.IXMLDOMDocument2，在使用该参数之前，请判断是否为空
            //MSXML2.IXMLDOMDocument2 CurDomRet = Convert.ToObject(broker.GetResult("CurDom"));
            //到货单接口返回result += JsonConvert.SerializeObject(broker.GetResult("CurDom"));


            //获取返回值
            //获取普通返回值。此返回值数据类型为System.Boolean，此参数按值传递，表示返回值:true:成功,false:失败
            System.Boolean result = Convert.ToBoolean(broker.GetReturnValue());
           
            //获取out/inout参数值

            //获取普通OUT参数errMsg。此返回值数据类型为System.String，在使用该参数之前，请判断是否为空
            System.String errMsgRet = broker.GetResult("errMsg") as System.String;
            材料出库单接口返回result = errMsgRet;
            //获取普通INOUT参数VouchId。此返回值数据类型为System.String，在使用该参数之前，请判断是否为空
            System.String VouchIdRet = broker.GetResult("VouchId") as System.String;
            rdrecord11Id = VouchIdRet;
            //获取普通OUT参数domMsg。此返回值数据类型为MSXML2.IXMLDOMDocument2，在使用该参数之前，请判断是否为空
            //MSXML2.IXMLDOMDocument2 domMsgRet = Convert.ToObject(broker.GetResult("domMsg"));

            //材料出库单接口返回result += JsonConvert.SerializeObject(broker.GetResult("domMsg"));
            //结束本次调用，释放API资源
            broker.Release();
            return result;

        }

       

        private static int 材料出库头赋值(string strHeadName, int id, List<Z_任务单实际投入> z_任务单实际投入List)
        {
            Z_任务单计划头 z_任务单计划头 = SqlSugarHelp.scope.Queryable<Z_任务单计划头>().Where(it => it.任务单guid == z_任务单实际投入List[0].任务单guid).First();
            Warehouse warehouse = SqlSugarHelp.scope.Queryable<Warehouse>().Where(it => it.cWhCode == z_任务单实际投入List[0].仓库编号).First();
            RecordOutQ recordOutQ = new RecordOutQ();
            recordOutQ.id = id;
            recordOutQ.brdflag = 0;
            recordOutQ.cvouchtype = "11";
            recordOutQ.csysbarcode = "||st11|202406241299";
            recordOutQ.cwhcode = z_任务单实际投入List[0].仓库编号;
            if (z_任务单实际投入List[0].仓库编号 == "0201" || z_任务单实际投入List[0].仓库编号 == "0202")
            {
                if (z_任务单计划头.包装日期 != null)
                {
                    recordOutQ.ddate = (DateTime)z_任务单计划头.包装日期;
                }
            }
            else
            {
                if (z_任务单计划头.任务日期 != null)
                {
                    recordOutQ.ddate = (DateTime)z_任务单计划头.任务日期;
                }
            }
            //去掉，用U8自动编号
            //recordOutQ.ccode = getMaxCode();
            //编码规则是 仓库+日期+4位流水号
            recordOutQ.ccode = getCode(recordOutQ.ddate.ToString("yyMM"), z_任务单实际投入List[0].仓库编号);
            //出库类别编码 葆寿堂为空
            if (z_任务单实际投入List[0].统货分拣 == "统货分拣")
            {
                
                    recordOutQ.crdcode = "0208";
            }
            else
            {
                recordOutQ.crdcode = "0201";
            }
            
            recordOutQ.cdepcode = "06";
            //recordOutQ.cpersoncode = "NULL";
            //recordOutQ.cprobatch = "NULL";
            //recordOutQ.chandler = z_任务单实际投入List[0].创建人;
            //recordOutQ.cmemo = "NULL";
            //recordOutQ.caccounter = "NULL";
            recordOutQ.cmaker = z_任务单实际投入List[0].创建人;
            //recordOutQ.cdefine1 = "NULL";
            //recordOutQ.cdefine2 = "NULL";
            //recordOutQ.cdefine3 = "NULL";
            //recordOutQ.cdefine4 = "NULL";
            //recordOutQ.cdefine5 = "NULL";
            //recordOutQ.cdefine6 = "NULL";
            //recordOutQ.cdefine7 = "NULL";
            //recordOutQ.cdefine8 = "NULL";
            //recordOutQ.cdefine9 = "NULL";
            //recordOutQ.cdefine10 = "NULL";
            recordOutQ.cwhname = warehouse.cWhName;
            //recordOutQ.cdepname = "NULL";
            //recordOutQ.cpersonname = "NULL";
            //出库类别  葆寿堂为空
            //recordOutQ.crdname = "材料领用";

            recordOutQ.csource = "库存";
            recordOutQ.cbustype = "领料";
            //recordOutQ.cbuscode = "NULL";
            //审核日期 
            //recordOutQ.dveridate = "2019-08-02 00:00:00.000";
            //recordOutQ.darvdate = "NULL";
            //recordOutQ.cchkcode = "NULL";
            //recordOutQ.dchkdate = "NULL";
            //recordOutQ.cchkperson = "NULL";
            recordOutQ.bisstqc = false;
            //模版号 
            recordOutQ.vt_id = 65;
            //recordOutQ.cdefine11 = "NULL";
            //recordOutQ.cdefine12 = "NULL";
            //recordOutQ.cdefine13 = "NULL";
            //recordOutQ.cdefine14 = "NULL";
            //recordOutQ.cdefine15 = "NULL";
            //recordOutQ.cdefine16 = "NULL";
            //产量 
            recordOutQ.imquantity = 0;
            //recordOutQ.cpspcode = "NULL";
            //recordOutQ.cinvname = "NULL";
            //recordOutQ.cinvstd = "NULL";
            //recordOutQ.cinvaddcode = "NULL";
            //recordOutQ.hcinvdefine1 = "NULL";
            //recordOutQ.hcinvdefine2 = "NULL";
            //recordOutQ.hcinvdefine3 = "NULL";
            //recordOutQ.hcinvdefine4 = "NULL";
            //recordOutQ.hcinvdefine5 = "NULL";
            //recordOutQ.hcinvdefine6 = "NULL";
            //recordOutQ.hcinvdefine7 = "NULL";
            //recordOutQ.hcinvdefine8 = "NULL";
            //recordOutQ.hcinvdefine9 = "NULL";
            //recordOutQ.hcinvdefine10 = "NULL";
            //recordOutQ.hcinvdefine11 = "NULL";
            //recordOutQ.hcinvdefine12 = "NULL";
            //recordOutQ.hcinvdefine13 = "NULL";
            //recordOutQ.hcinvdefine14 = "NULL";
            //recordOutQ.hcinvdefine15 = "NULL";
            //recordOutQ.hcinvdefine16 = "NULL";
            //recordOutQ.cmpocode = "0000000017";
            recordOutQ.bpufirst = false;
            recordOutQ.biafirst = false;
            //recordOutQ.cvencode = "NULL";
            //recordOutQ.cvenabbname = "NULL";
            //recordOutQ.ivenauthid = "NULL";
            //recordOutQ.ipurorderid = "NULL";
            //生产订单ID 
            recordOutQ.iproorderid = 0;
            recordOutQ.biscomplement = 0;
            //recordOutQ.ipspauthid = "NULL";
            //recordOutQ.cproinvaddcode = "NULL";
            recordOutQ.ireturncount = 0;
            recordOutQ.iverifystate = 0;
            recordOutQ.iswfcontrolled = 0;
            recordOutQ.ufts = "                    27270.1661";
            recordOutQ.cmodifyperson = "";
            //recordOutQ.dmodifydate = "NULL";
            //制单时间 
            recordOutQ.dnmaketime = DateTime.Now;
            //recordOutQ.dnmodifytime = "NULL";
            //recordOutQ.dnverifytime = "2019-08-02 08:25:27.220";
            recordOutQ.bomfirst = 0;
            recordOutQ.bmotran = 0;
            recordOutQ.chinvsn = "";
            recordOutQ.isourcerowno = "";
            //recordOutQ.iflowid = "NULL";
            //recordOutQ.cflowname = "NULL";
            recordOutQ.bhyvouch = false;
            recordOutQ.iPrintCount = 0;
            //recordOutQ.ccurrentauditor = "NULL";
            //recordOutQ.cfactorycode = "NULL";
            //recordOutQ.cfactoryname = "NULL";
            //recordOutQ.cchecksignflag = "33C73C0C2D63397A0B83FCCE92D96335";
          return  recordOutQ.Add(strHeadName);
        }

        private static string getCode(string 年月, string 仓库号)
        {
            string sql = $@"select  isnull(max(right(cCode,4)),'0000') from RecordOutQ where cCode like '{年月}{仓库号}%' ";
            int code = SqlSugarHelp.scope.Ado.GetInt(sql);
            code++;
            return $"{年月}{仓库号}{code.ToString().PadLeft(4, '0')}";
        }

        private static int 材料出库体赋值(string strBodyName, int id, List<Z_任务单实际投入> z_任务单实际投入List)
        {
            foreach (var item in z_任务单实际投入List)
            {
                Inventory inventory = SqlSugarHelp.scope.Queryable<Inventory>().Where(it => it.cInvCode == item.投入物料编号).First();

                recordoutsq recordOutSQ = new recordoutsq();
            recordOutSQ.autoid = (int)SnowFlakeSingle.Instance.NextId(); 
            recordOutSQ.id = id;
            recordOutSQ.cinvcode = item.投入物料编号;
            recordOutSQ.cinvaddcode = inventory.cInvAddCode;
            recordOutSQ.cinvname = inventory.cInvName;
            recordOutSQ.cinvstd = inventory.cInvStd;
            recordOutSQ.cinvccode = inventory.cInvCCode;
            recordOutSQ.cinvm_unit = inventory.cComUnitCode;
                //recordOutSQ.cinva_unit = "NULL";
                //recordOutSQ.creplaceitem = "NULL";
                //recordOutSQ.cposition = "NULL";
                //recordOutSQ.cinvdefine1 = item.统货拣货次数;
                //recordOutSQ.cinvdefine2 = "6000";
                //recordOutSQ.cinvdefine3 = "6061 T6";
                //recordOutSQ.cinvdefine4 = "NULL";
                //recordOutSQ.cinvdefine5 = "1.012";
                //recordOutSQ.cinvdefine6 = "韦氏硬度≥14";
                //recordOutSQ.cinvdefine7 = "NULL";
                //recordOutSQ.cinvdefine8 = "NULL";
                //recordOutSQ.cinvdefine9 = "NULL";
                //recordOutSQ.cinvdefine10 = "NULL";
                //recordOutSQ.cinvdefine11 = "NULL";
                //recordOutSQ.cinvdefine12 = "NULL";
                //recordOutSQ.cinvdefine13 = "NULL";
                //recordOutSQ.cinvdefine14 = "NULL";
                //recordOutSQ.cinvdefine15 = "NULL";
                //recordOutSQ.cinvdefine16 = "NULL";
                //recordOutSQ.iid = "NULL";
                if (rdrecord10Service.是否启用存货自由项统货拣货次数(inventory.cInvCode))
                {
                    recordOutSQ.cfree1 = item.统货拣货次数;
                }

                if (rdrecord10Service.是否启用存货自由项等级说明(inventory.cInvCode))
                {
                    recordOutSQ.cfree2 = item.等级说明;
                }
                //recordOutSQ.cfree2 = "NULL";
                //recordOutSQ.cfree3 = "NULL";
                //recordOutSQ.cfree4 = "NULL";
                //recordOutSQ.cfree5 = "NULL";
                //recordOutSQ.cfree6 = "NULL";
                //recordOutSQ.cfree7 = "NULL";
                //recordOutSQ.cfree8 = "NULL";
                //recordOutSQ.cfree9 = "NULL";
                //recordOutSQ.cfree10 = "NULL";
                if (inventory.bInvBatch == true)
                {
                    recordOutSQ.cbatch = item.批号;
                }
                
            //recordOutSQ.iinvexchrate = "NULL";
            //recordOutSQ.inum = "NULL";
            recordOutSQ.iquantity = item.投入数量;
                //recordOutSQ.iunitcost = "NULL";
                //recordOutSQ.iprice = "NULL";
                //recordOutSQ.cbarcode = "NULL";
                //recordOutSQ.ipunitcost = "NULL";
                //recordOutSQ.ipprice = "NULL";
                //recordOutSQ.dvdate = "NULL";
                //recordOutSQ.cobjcode = "NULL";
                //recordOutSQ.isoutquantity = "NULL";
                //recordOutSQ.isoutnum = "NULL";
                //recordOutSQ.inquantity = "8.3300000000";
                //recordOutSQ.innum = "NULL";
                //recordOutSQ.cassunit = "NULL";
                //recordOutSQ.dmadedate = "NULL";
                //recordOutSQ.ifquantity = "NULL";
                //recordOutSQ.ifnum = "NULL";
                //recordOutSQ.cvouchcode = "NULL";
                //任务单材料行
                recordOutSQ.cdefine22 = item.任务单号;
                //任务单材料行次
                recordOutSQ.cdefine23 = item.实际投入guid.ToString();
                //recordOutSQ.cdefine24 = "NULL";
                //recordOutSQ.cdefine25 = "NULL";
                //recordOutSQ.cdefine26 = "NULL";
                //recordOutSQ.cdefine27 = "NULL";
                //recordOutSQ.cdefine28 = "NULL";
                //recordOutSQ.cdefine29 = "NULL";
                //recordOutSQ.cdefine30 = "NULL";
                //recordOutSQ.cdefine31 = "NULL";
                //recordOutSQ.cdefine32 = "NULL";
                //recordOutSQ.cdefine33 = "NULL";
                //recordOutSQ.cdefine34 = "NULL";
                //recordOutSQ.cdefine35 = "NULL";
                //recordOutSQ.cdefine36 = "NULL";
                //recordOutSQ.cdefine37 = "NULL";
                //recordOutSQ.impoids = "1000001403";
                //recordOutSQ.icheckids = "NULL";
                //recordOutSQ.citemcode = "NULL";
                //recordOutSQ.citem_class = "NULL";
                //recordOutSQ.cname = "NULL";
                //recordOutSQ.citemcname = "NULL";
                //recordOutSQ.cbvencode = "NULL";
                //recordOutSQ.cvenname = "NULL";
                //recordOutSQ.imassdate = "NULL";
                //recordOutSQ.cinvouchcode = "NULL";
                //recordOutSQ.imaterialfee = "NULL";
                //recordOutSQ.iomomid = "NULL";
                //recordOutSQ.iomodid = "NULL";
                //recordOutSQ.comcode = "NULL";
                //recordOutSQ.cmassunit = "NULL";
                //recordOutSQ.cposname = "NULL";
                //recordOutSQ.isquantity = "NULL";
                //recordOutSQ.ismaterialfee = "NULL";
                //recordOutSQ.dmsdate = "NULL";
                //recordOutSQ.cmocode = "0000000010";
                //recordOutSQ.imoseq = "28";
                //recordOutSQ.cmolotcode = "NULL";
                //recordOutSQ.iopseq = "0010";
                //recordOutSQ.copdesc = "锯切";
                //recordOutSQ.cmworkcentercode = "02";
                //recordOutSQ.cmworkcenter = "生产部";
                //recordOutSQ.invcode = "0402020002";
                //recordOutSQ.invname = "480竖管";
                //recordOutSQ.invstd = "Φ48.4*2.6";
                //recordOutSQ.corufts = "";
                //recordOutSQ.isodid = "1000000238";
                //需求跟踪行号
                recordOutSQ.isotype = 0;
                //recordOutSQ.csocode = "SO2760";
                //recordOutSQ.isoseq = 0;
            recordOutSQ.cdemandmemo = "";
                //销售订单类别 
                recordOutSQ.iordertype = 0;
            //recordOutSQ.iorderdid = "1000000238";
            //recordOutSQ.iordercode = "SO2760";
            //recordOutSQ.iorderseq = "1";
            //recordOutSQ.cbaccounter = "NULL";
            recordOutSQ.bcosting = true;
                //recordOutSQ.dbkeepdate = "NULL";
                //recordOutSQ.cvmivencode = "NULL";
                //recordOutSQ.cvmivenname = "NULL";
                //代管消耗标识 
                recordOutSQ.bvmiused = false;
            //recordOutSQ.ivmisettlequantity = "NULL";
            //recordOutSQ.ivmisettlenum = "NULL";
            //recordOutSQ.imaids = "NULL";
            //recordOutSQ.iinvsncount = "NULL";
            //recordOutSQ.iuninvsncount = "NULL";
            //recordOutSQ.cwhpersoncode = "NULL";
            //recordOutSQ.cwhpersonname = "NULL";
            //recordOutSQ.crejectcode = "NULL";
            recordOutSQ.iexpiratdatecalcu = 0;
            //recordOutSQ.cexpirationdate = "NULL";
            //recordOutSQ.dexpirationdate = "NULL";
            //recordOutSQ.cciqbookcode = "NULL";
            //recordOutSQ.ibondedsumqty = "NULL";
            //recordOutSQ.productinids = "NULL";
            //recordOutSQ.cbatchproperty1 = "NULL";
            //recordOutSQ.cbatchproperty2 = "NULL";
            //recordOutSQ.cbatchproperty3 = "NULL";
            //recordOutSQ.cbatchproperty4 = "NULL";
            //recordOutSQ.cbatchproperty5 = "NULL";
            //recordOutSQ.cbatchproperty6 = "NULL";
            //recordOutSQ.cbatchproperty7 = "NULL";
            //recordOutSQ.cbatchproperty8 = "NULL";
            //recordOutSQ.cbatchproperty9 = "NULL";
            //recordOutSQ.cbatchproperty10 = "NULL";
            //recordOutSQ.irsrowno = "NULL";
            //recordOutSQ.cbmemo = "NULL";
            //recordOutSQ.cbinvsn = "";
            //recordOutSQ.applydid = "NULL";
            //recordOutSQ.applycode = "NULL";
            recordOutSQ.irowno = z_任务单实际投入List.IndexOf(item)+1;
                //recordOutSQ.strowguid = "NULL";
                //recordOutSQ.vmiveniid = "NULL";
                //recordOutSQ.cservicecode = "NULL";
                //recordOutSQ.bveniid = "NULL";
                //recordOutSQ.cinvouchtype = "NULL";
                //recordOutSQ.coutvouchid = "NULL";
                //recordOutSQ.coutvouchtype = "NULL";
                //recordOutSQ.isredoutquantity = "NULL";
                //recordOutSQ.isredoutnum = "NULL";
                //recordOutSQ.itrids = "NULL";
                //recordOutSQ.ipesodid = "1000001403";
                //0
                recordOutSQ.ipesotype = 0;
            //recordOutSQ.cpesocode = "0000000010";
            //recordOutSQ.ipesoseq = "28";
            //recordOutSQ.bsupersede = "NULL";
            //recordOutSQ.isupersedeqty = "NULL";
            //recordOutSQ.isupersedempoids = "NULL";
            //recordOutSQ.imoallocatesubid = "NULL";
            //recordOutSQ.cinvouchercode = "NULL";
            //recordOutSQ.cinvoucherlineid = "NULL";
            //recordOutSQ.cinvouchertype = "NULL";
            recordOutSQ.cbsysbarcode = "||st11|201907290002|1";
            //recordOutSQ.csourcemocode = "NULL";
            //recordOutSQ.isourcemodetailsid = "NULL";
            //recordOutSQ.cplanlotcode = "SASO27541";
            recordOutSQ.bcanreplace = 0;
            //recordOutSQ.iposflag = "NULL";
            recordOutSQ.boutmaterials = 0;
            //recordOutSQ.iavaquantity = "NULL";
            //recordOutSQ.iavanum = "NULL";
            //recordOutSQ.ipresent = "NULL";
            //recordOutSQ.ipresentnum = "NULL";
            //recordOutSQ.supersedesrc = "NULL";
            //recordOutSQ.supersedebatch = "NULL";

            recordOutSQ.Add(strBodyName);
            }
            return 1;
        }

        private static string getMaxCode()
        {
            string sql = @"select  isnull(max(right(cCode,4)),'0000') from RecordOutQ
						-- where DATEDIFF(MONTH,dDate ,GETDATE()) = 0";
            int code = SqlSugarHelp.scope.Ado.GetInt(sql);
            code++;
            return code.ToString().PadLeft(8, '0');
        }

        public static bool 下一个统货拣货次数(string 物料编号,string 当前统货次数,ref string 下一统货次数)
        {
            string sql = $@" select cUDValue from AA_InvFreeContrapose where cinvcode = '{物料编号}' order by pubufts ";
            List<string> 所有统货拣货次数 = SqlSugarHelp.scope.Ado.SqlQuery<string>(sql);
            //根据任务单实际投入已推送的统货拣货次数，查所有统货拣货次数的下一个统货拣货次数

            // 找到下一个次序的逻辑
            //string 下一个统货拣货次数 = null;
            bool 是最后一个 = false;

            // 判断是否找到
            if (所有统货拣货次数.Contains(当前统货次数))
            {
                int 当前索引 = 所有统货拣货次数.IndexOf(当前统货次数);

                // 判断是否最后一个元素
                if (当前索引 == 所有统货拣货次数.Count - 1)
                {
                    下一统货次数 =  $"给定统货拣货次数:{当前统货次数}已经是最后一个";
                    是最后一个 = true;
                }
                else
                {
                    // 获取下一个元素
                    下一统货次数 = 所有统货拣货次数[当前索引 + 1];
                }
            }
            else
            {
                下一统货次数 = $"U8统货次数档案中未找到{当前统货次数}";
                是最后一个 = true;
            }

            return 是最后一个;
        }
    }
}